import api from './auth.js'

// 商家管理API - 复用admin API但限制功能范围
export const merchantAPI = {
  // 图书管理
  getBookList: (params) => {
    return api.get('/admin/books', { params })
  },

  createBook: (bookData) => {
    return api.post('/admin/books', bookData)
  },

  updateBook: (bookId, bookData) => {
    return api.put(`/admin/books/${bookId}`, bookData)
  },

  deleteBook: (bookId) => {
    return api.delete(`/admin/books/${bookId}`)
  },

  toggleBookStatus: (bookId, status) => {
    return api.patch(`/admin/books/${bookId}/status`, { status })
  },

  batchDeleteBooks: (bookIds) => {
    return api.delete('/admin/books/batch', { data: { ids: bookIds } })
  },

  getAvailableBooks: (params) => {
    return api.get('/admin/books/available', { params })
  },

  // 分类管理
  getCategoryList: (params) => {
    return api.get('/admin/categories', { params })
  },

  createCategory: (categoryData) => {
    return api.post('/admin/categories', categoryData)
  },

  updateCategory: (categoryId, categoryData) => {
    return api.put(`/admin/categories/${categoryId}`, categoryData)
  },

  deleteCategory: (categoryId) => {
    return api.delete(`/admin/categories/${categoryId}`)
  },

  toggleCategoryStatus: (categoryId, status) => {
    return api.patch(`/admin/categories/${categoryId}/status`, { status })
  },

  // 标签管理
  getTagList: (params) => {
    return api.get('/admin/tags', { params })
  },

  getAllTags: () => {
    return api.get('/admin/tags/all')
  },

  createTag: (tagData) => {
    return api.post('/admin/tags', tagData)
  },

  updateTag: (tagId, tagData) => {
    return api.put(`/admin/tags/${tagId}`, tagData)
  },

  deleteTag: (tagId) => {
    return api.delete(`/admin/tags/${tagId}`)
  },

  // 畅销管理
  getBestsellerList: (params) => {
    return api.get('/admin/bestsellers', { params })
  },

  createBestseller: (bestsellerData) => {
    return api.post('/admin/bestsellers', bestsellerData)
  },

  updateBestseller: (bestsellerId, bestsellerData) => {
    return api.put(`/admin/bestsellers/${bestsellerId}`, bestsellerData)
  },

  deleteBestseller: (bestsellerId) => {
    return api.delete(`/admin/bestsellers/${bestsellerId}`)
  },

  toggleBestsellerStatus: (bestsellerId, status) => {
    return api.patch(`/admin/bestsellers/${bestsellerId}/status`, { status })
  },

  // 生成畅销榜单
  generateBestsellerRankings: () => {
    return api.get('/admin/bestsellers/generate')
  },

  // 订单管理
  getOrderList: (params) => {
    return api.get('/admin/orders', { params })
  },

  getOrderDetail: (orderId) => {
    return api.get(`/admin/orders/${orderId}`)
  },

  updateOrderStatus: (orderId, status) => {
    return api.patch(`/admin/orders/${orderId}/status`, { status })
  },

  shipOrder: (orderId, shipData) => {
    return api.post(`/admin/orders/${orderId}/ship`, shipData)
  },

  cancelOrder: (orderId, reason) => {
    return api.post(`/admin/orders/${orderId}/cancel`, { reason })
  },

  // 文件上传
  uploadFile: (formData) => {
    return api.post('/admin/upload', formData, {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })
  },

  // 数据导出
  exportBooks: (params) => {
    return api.get('/admin/books/export', {
      params,
      responseType: 'blob'
    })
  },

  exportCategories: (params) => {
    return api.get('/admin/categories/export', {
      params,
      responseType: 'blob'
    })
  },

  exportOrders: (params) => {
    return api.get('/admin/orders/export', {
      params,
      responseType: 'blob'
    })
  },

  // 商家业务统计
  getBusinessStats: () => {
    return api.get('/merchant/business/stats')
  },

  // 商家个人资料
  getMerchantProfile: () => {
    return api.get('/merchant/profile')
  },

  updateMerchantProfile: (profileData) => {
    return api.put('/merchant/profile', profileData)
  },

  changeMerchantPassword: (passwordData) => {
    return api.put('/merchant/password', passwordData)
  },

  getMerchantStats: () => {
    return api.get('/merchant/profile/stats')
  },

  // 商家头像上传
  uploadAvatar: (formData) => {
    return api.post('/merchant/profile/avatar', formData, {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })
  }
}

export default merchantAPI
